package org.tobuy.dao.proxy;
import java.util.ArrayList;
import java.util.List;


import org.tobuy.dao.IItemDAO;
import org.tobuy.dao.impl.ItemDAOImpl;
import org.tobuy.dbc.DatabaseConnection;
import org.tobuy.vo.Book;
import org.tobuy.vo.Dvd;
import org.tobuy.vo.Game;
import org.tobuy.vo.Item;
import org.tobuy.vo.User;


public class ItemDAOProxy implements IItemDAO{
	private DatabaseConnection dbc= null;
	private IItemDAO dao = null; 
	
	public ItemDAOProxy(){
		try{
			this.dbc = new DatabaseConnection();
		}catch(Exception e){
			e.printStackTrace();
		}
		this.dao = new ItemDAOImpl(dbc.getConnection());
	}

	public List<Item> findItems() throws Exception {
		List<Item> items = new ArrayList<Item>();
		try{
			items = this.dao.findItems();
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return items;
	}
	
	public List<Item> findOnSellItems(User user) throws Exception {
		List<Item> items = new ArrayList<Item>();
		try{
			items = this.dao.findOnSellItems(user);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return items;
	}

	public List<Item> findItems(String type) throws Exception {
		List<Item> items = new ArrayList<Item>();
		try{
			items = this.dao.findItems(type);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return items;
	}
	
	public Item findItem(String item_id) throws Exception {
		Item item = new Item();
		try{
			item = this.dao.findItem(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return item;
	}
	public Book findBook(String item_id) throws Exception {
		Book book = new Book();
		try{
			book = this.dao.findBook(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return book;
	}

	public Game findGame(String item_id) throws Exception {
		Game game = new Game();
		try{
			game = this.dao.findGame(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return game;
	}

	public Dvd findDvd(String item_id) throws Exception {
		Dvd dvd = new Dvd();
		try{
			dvd = this.dao.findDvd(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return dvd;
	}

	public boolean deleteItem(String item_id) throws Exception {
		boolean flag = false;
		try{
			flag = this.dao.deleteItem(item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public boolean changeAmount(int amount, String item_id) throws Exception {
		boolean flag = false;
		try{
			flag = this.dao.changeAmount(amount,item_id);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}



	public int addItem(Book book, User user) throws Exception {
		int flag = -1;
		try{
			flag = this.dao.addItem(book,user);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public int addItem(Game game, User user) throws Exception {
		int flag = -1;
		try{
			flag = this.dao.addItem(game,user);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public int addItem(Dvd dvd, User user) throws Exception {
		int flag = -1;
		try{
			flag = this.dao.addItem(dvd,user);
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return flag;
	}

	public int last_insert_id() throws Exception {
		int last_insert_id;
		try{
			last_insert_id = this.dao.last_insert_id();
		}catch(Exception e){
			throw e;
		}finally{
			this.dbc.close();
		}
		return last_insert_id;
	}

	




}
